﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DevExpress.Utils.Win;
using mendian;
using DiaoPaiDaYin;
using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.UC.门店下单.DTO;
using DXApplicationTangche.service;
using static DXApplicationTangche.UC.门店下单.form.Frm门店统一下单;
using DXApplicationTangche.model;
using DXApplicationTangche.DTO;
using DXApplicationTangche.UC.销售管理;
using DXApplicationTangche.UC.样品管理.样品下单新;
using DXApplicationTangche.UC.销售记录;
using static DXApplicationTangche.UC.门店下单.form.Frm订单一览;
using DXApplicationTangche.UC.销售记录.service;
using DXApplicationTangche.UC.样品管理.样品下单新.service;
using DXApplicationTangche.UC.门店出入库.淘宝;
using DXApplicationTangche.UC.样品管理.样品下单;
using DXApplicationTangche.UC.订单一览新.订单预览新.service;

namespace DXApplicationTangche.UC.门店下单.form
{
    public partial class Frm样品下单 : DevExpress.XtraEditors.XtraForm
    {
        /// <summary>
        /// 订单充血类
        /// </summary>
        private OrderModel orderModel = new OrderModel();
        public DTO客户店铺 dTO;
        public 样品管理.样品下单新.service.CustomerChooseInfoDTO customerChooseInfoDTO = new 样品管理.样品下单新.service.CustomerChooseInfoDTO();

        public Frm样品下单()
        {
            InitializeComponent();
            CustomerService.customer置空();
            //this.initData();    //  测试方法
            this.gridControl订单分录一览.DataSource = this.orderModel.orderCommitDTO返回s;
        }

        /// <summary>
        /// 辅料信息注入
        /// </summary>
        /// <param name="dto定制下单"></param>
        /// <param name="dto辅料"></param>
        public void buildOrderModel(Dto定制下单 dto定制下单, StyleAccessoryItemsDTO dto辅料, DXApplicationTangche.model.Enum订单类型 @enum)
        {
            this.orderModel.buildAddOrderDtos(dto定制下单,dto辅料, @enum);
        }

        private void Frm样品下单_Load(object sender, EventArgs e)
        {
            //Frm客户.customer_countid = 4880;
            //Frm客户.cUSTOMER_ID = 4841;
            //Frm客户.customer_name = "朱晓东";
            //Frm客户.aDDRESS_ID = 3740;
            //this.searchLookUpEdit1.Properties.DataSource = FunctionHelper.JsonDeserialization<List<DTO客户店铺>>(RestCall.httpGetMethod(URL.接口url + "/customer/getBusinessPartnerAll"));
            //this.gridControl2.DataSource = CustomerService.GetCustomerInformation(Frm客户.cUSTOMER_ID);
            this.searchLookUpEdit1.Properties.DataSource = 样品下单Service.getOutSourcingCustomer();
        }
        #region 选择用户
        private void searchLookUpEdit1_Popup(object sender, EventArgs e)
        {
            //得到当前SearchLookUpEdit弹出窗体
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clearBtn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }

        private void searchLookUpEdit1View_Click(object sender, EventArgs e)
        {
            var a = this.searchLookUpEdit1.Properties.View.GetSelectedRows();
            foreach (int rowHandle in a)
            {
                this.customerChooseInfoDTO.customerId //  no
                    = this.searchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "customerId").ToString();//id 是 Value Member
                this.customerChooseInfoDTO.customerName //  no
                    = this.searchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "customerName").ToString();//id 是 Value Member
                this.customerChooseInfoDTO.mobile
                    = this.searchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "mobile").ToString();//id 是 Value Member
                try
                {
                    //this.customerChooseInfoDTO.addressId //  no
                    //= this.searchLookUpEdit1.Properties.View.GetRowCellValue(rowHandle, "addressId").ToString();//id 是 Value Member
                }
                catch {}
            }
            this.orderModel = new OrderModel();
            this.refreshGridControl();
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clearBtn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit1.ToolTip = null;
            searchLookUpEdit1.EditValue = null;
        }
        private void searchLookUpEdit1_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.customerChooseInfoDTO.customerName;
        }
        #endregion

        private void tileBarItem1_ItemClick(object sender, TileItemEventArgs e)
        {
            //new Frm样品下单选款式(this,Enum下单类型.服装定制下单).ShowDialog();
            //new Frm样品下单选款式新(this).ShowDialog();
            new Form样品下单详情(new FunctionHelper().Uuid,this).ShowDialog();
        }

        public void refreshGridControl()
        {
            this.gridControl订单分录一览.DataSource = this.orderModel.orderCommitDTO返回s;
            this.tileView1.RefreshData();
        }

        private void siveorder_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(this.customerChooseInfoDTO.customerId))
            {
                MessageBox.Show("客户为空,请选择客户");
                return;
            }
            foreach (OrderCommitDTO返回 orderDto in this.orderModel.orderCommitDTO返回s)
            {
                orderDto.customerId = this.customerChooseInfoDTO.customerId.Trim();
                orderDto.customerCountId = Convert.ToString(1);
                orderDto.customerName = this.customerChooseInfoDTO.customerName.Trim();
                orderDto.consignee = this.customerChooseInfoDTO.customerName.Trim();
                orderDto.addressId = this.customerChooseInfoDTO.addressId;
                orderDto.mobile = this.customerChooseInfoDTO.mobile.Trim();
                if ("春衫定制".Equals(orderDto.customerName.Trim()))
                {
                    orderDto.salesType = "样品下单";
                    orderDto.salesChannel = "春衫定制";
                }
                else
                {
                    orderDto.salesType = "受托加工";
                    orderDto.salesChannel = orderDto.customerName.Trim();
                }
            }
            //new Form销售记录(this.orderModel, Frm客户.customer_name, Enum订单类别.样品订单).ShowDialog();
            //样品下单不需要销售确认
            销售记录Service.post销售记录(
                new DTO销售记录().build样衣(orderModel.orderCommitDTO返回s),
                Enum订单类别.样品订单
                );
            MessageBox.Show("保存成功");
            this.Close();
        }

        private void simpleButton创建客户_Click(object sender, EventArgs e)
        {
            new Form创建委外客户().ShowDialog();
            this.searchLookUpEdit1.Properties.DataSource = 样品下单Service.getOutSourcingCustomer();
            this.searchLookUpEdit1.Refresh();
        }
    }
}